package com.liy.framework.common.orm.mybatis.interceptor;

import com.liy.framework.common.vo.PageVO;
import org.apache.ibatis.executor.resultset.*;
import java.sql.*;
import org.apache.ibatis.session.*;
import org.apache.ibatis.plugin.*;
import java.util.*;
import org.apache.ibatis.reflection.factory.*;
import org.apache.ibatis.reflection.wrapper.*;
import org.apache.ibatis.reflection.*;


@Intercepts({ @Signature(type = ResultSetHandler.class, method = "handleResultSets", args = { Statement.class }) })
public class PaginationResultSetHandlerInterceptor implements Interceptor
{
    private static final ObjectFactory DEFAULT_OBJECT_FACTORY;
    private static final ObjectWrapperFactory DEFAULT_OBJECT_WRAPPER_FACTORY;
    private static final ReflectorFactory DEFAULT_REFLECTOR_FACTORY;

    public Object intercept(final Invocation invocation) throws Throwable {
        final ResultSetHandler resultSetHandler = (ResultSetHandler)invocation.getTarget();
        final MetaObject metaStatementHandler = MetaObject.forObject((Object)resultSetHandler, PaginationResultSetHandlerInterceptor.DEFAULT_OBJECT_FACTORY, PaginationResultSetHandlerInterceptor.DEFAULT_OBJECT_WRAPPER_FACTORY, PaginationResultSetHandlerInterceptor.DEFAULT_REFLECTOR_FACTORY);
        final RowBounds rowBounds = (RowBounds)metaStatementHandler.getValue("rowBounds");
        final Object result = invocation.proceed();
        if (rowBounds instanceof PageVO) {
            metaStatementHandler.setValue("rowBounds.results", result);
        }
        return result;
    }

    public Object plugin(final Object target) {
        return Plugin.wrap(target, (Interceptor)this);
    }

    public void setProperties(final Properties properties) {
    }

    static {
        DEFAULT_OBJECT_FACTORY = (ObjectFactory)new DefaultObjectFactory();
        DEFAULT_OBJECT_WRAPPER_FACTORY = (ObjectWrapperFactory)new DefaultObjectWrapperFactory();
        DEFAULT_REFLECTOR_FACTORY = (ReflectorFactory)new DefaultReflectorFactory();
    }
}
